package com.pe.pgn.clubpgn.webapp.controller.ingresoarticulo;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.domain.ClpbAlmacen;
import com.pe.pgn.clubpgn.domain.ClpbCategoria;
import com.pe.pgn.clubpgn.domain.beans.BNArticulo;
import com.pe.pgn.clubpgn.domain.beans.BNDocumentoMovArticulo;
import com.pe.pgn.clubpgn.service.CategoriaService;
import com.pe.pgn.clubpgn.service.IngresoArticuloService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;
import com.pe.pgn.clubpgn.webapp.util.CalendarUtil;

/**
 * @author Frank
 * */

@Controller
@RequestMapping("/ingresoarticulo/**")
public class IngresoArticuloController extends BaseFormController{

	@Autowired
	private IngresoArticuloService ingresoArticuloService;
	
	@Autowired
	private CategoriaService categoriaService;
	
	@RequestMapping(value="/ingresoarticulolist.html")
	public void listarArticulos(Model model){
		
		List<BNDocumentoMovArticulo> listaarticulo = ingresoArticuloService.obtenerTodosArticulos();		
		List<ClpbAlmacen> listaalmacen = ingresoArticuloService.obtenerTodosAlmacenConStock();		
		
		model.addAttribute("listaarticulo", listaarticulo);
		model.addAttribute("listaalmacen" , listaalmacen);
	}
	
	@RequestMapping(value = "/ingresoarticuloform.html", method = RequestMethod.GET)
	public @ModelAttribute("ingresoarticuloform") BNDocumentoMovArticulo verArticuloIngresado(
		   @RequestParam(value = "id", required = false) Long id, Model model) {
		
		BNDocumentoMovArticulo bnArticulo  = new BNDocumentoMovArticulo();
		if (id != null) { //modo update
			
			bnArticulo = ingresoArticuloService.obtenerArticuloIngresado(id);
			
		}else{ //modo insert
			
			bnArticulo.setDaFechaDocumento(CalendarUtil.calendarToString
			(CLPConstantes.DATE_PATTERN, Calendar.getInstance())); //seteando la fecha Actual
			
			List<ClpbAlmacen> listaalmacen = 
			ingresoArticuloService.obtenerTodosAlmacenConStock();		
			model.addAttribute("listaalmacen"   ,  listaalmacen);
		}
		
		
		model.addAttribute("ingresoarticuloform" , bnArticulo);
		return bnArticulo;	
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/ingresoarticuloform.html", method = RequestMethod.POST)
	public String guardarArticuloIngresado(
		@ModelAttribute("ingresoarticuloform") BNDocumentoMovArticulo bnArticulo,
		BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		Map map = ingresoArticuloService.
		guardarDocumentoIngresoArticulo(bnArticulo);
		
    	if(map.get("msg").equals("success")){    		
    		saveMessage(request, getText("ingresoarticuloForm.added", request.getLocale())); 
    		Long id = (Long)map.get("id");
    		return "redirect:ingresoarticuloform.html?id="+id+"&amp;from=list";
    	}else{
    		
    		List<ClpbAlmacen> listaalmacen = 
    		ingresoArticuloService.obtenerTodosAlmacenConStock();
    		
    		saveError(request, getText("errors.general", request.getLocale()));
    		model.addAttribute("listaalmacen" , listaalmacen);
    	}
		
    	model.addAttribute("ingresoarticuloform" , bnArticulo);
    	return "/ingresoarticulo/ingresoarticuloform";
	}
	
	@RequestMapping(value = "/buscararticulos.html", method = RequestMethod.GET)
	public String  buscarArticulos(Model model) {
		
		List<BNArticulo> articulos     = ingresoArticuloService.obtenerArticulosConStock();
		List<ClpbCategoria> categorias = categoriaService.obtenerTodoCategorias();
		
		model.addAttribute("listaarticulos"	  , articulos);
		model.addAttribute("listacategorias"  , categorias);
		return "/wizard/buscararticulowizardform";
	}
}
